#define _CRT_SECURE_NO_WARNINGS 2
#include<iostream>
#include<algorithm>

using namespace std;

//direct insert:ascending sort
//void InsertSort(int* arr, int n)
//{
//	for (int i = 1; i < n; i++)
//	{
//		for (int j = i-1; j>=0; j--)
//		{
//			if (arr[j + 1] < arr[j])
//			{
//				swap(arr[j + 1], arr[j]);
//			}
//			else break;
//		}
//	}
//}
//
//void ShellSort(int* arr, int n)
//{
//	int group = n/2;
//	while (group)
//	{
//		for (int i = 0; i < n-group; i++)
//		{
//			if (arr[i] > arr[i + group]) swap(arr[i], arr[i + group]);
//		}
//		group /= 2;
//	}
//}

void InsertSort(int* arr, int n)
{
	for (int i = 1; i < n; i++)
	{
		int end = i - 1;
		int tmp = arr[i];
		while (end >= 0)
		{
			if (arr[end] < tmp)
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
				break;
		}
		arr[end + 1] = tmp;
	}
}

void ShellSort(int* arr, int n)
{
	int group = n;
	while (group > 1)
	{
		group = group / 3 + 1;
		for (int i = 0; i < n - group; i++)
		{
			int end = i;
			int tmp = arr[i + group];
			while (end >= 0)
			{
				if (tmp < arr[end])
				{
					arr[end + group] = arr[end];
					end -= group;
				}
				else
					break;
			}
			arr[end + group] = tmp;
		}
	}

}


void Print(int* arr, int n)
{
	for (int i = 0; i < n; i++)
		cout << arr[i] << " ";
	cout << endl;
}

int main()
{
	int arr[] = { 4,5,9,-1,9,0 };
	ShellSort(arr, 6);
	Print(arr, 6);
	return 0;
}